package org.madbit.pingu;

import java.util.ArrayList;

import org.madbit.pingu.adapter.ConnectionsAdapter;
import org.madbit.pingu.entity.Connection;

import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ListView;
import android.widget.Toast;

public class Home extends ListActivity {
	/** Called when the activity is first created. */
	// ListView that will hold our items references back to main.xml
//	ListView lstTest;
	// Array Adapter that will hold our ArrayList and display the items on the ListView
	ConnectionsAdapter arrayAdapter;
	// List that will host our items and allow us to modify that array adapter
	ArrayList<Connection> connections = null;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// Initialize our ArrayList
		connections = new ArrayList<Connection>();

		// Initialize our array adapter notice how it references the listitems.xml layout
		arrayAdapter = new ConnectionsAdapter(Home.this, R.layout.main, connections);

		// Set the above adapter as the adapter of choice for our list lstTest.setAdapter(arrayAdapter);
		setListAdapter(arrayAdapter);
		// Now that we have that list lets add it to the ArrayList which will hold our items.
		// for(Connection c : dbConnections)
		// {
		// connections.add(l);
		// }

		// Since we've modified the arrayList we now need to notify the adapter that its data has changed so that it updates the UI
		Connection c1 = new Connection();
		c1.setIp("192.168.1.1");
		c1.setName("Server1");
		connections.add(c1);

		Connection c2 = new Connection();
		c2.setIp("192.168.2.2");
		c2.setName("Server2");
		connections.add(c2);

		arrayAdapter.notifyDataSetChanged();
		// Log.d("Error: ", e.getMessage());
	}
	
	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Connection c = (Connection) getListAdapter().getItem(position);		
		Intent connectionDetailsIntent = new Intent(this, ConnectionDetails.class);
		connectionDetailsIntent.putExtra("key", c.getName());
		this.startActivity(connectionDetailsIntent);
	}
}
